package top.shiyiri.web;

import top.shiyiri.pojo.Book;
import top.shiyiri.pojo.Page;
import top.shiyiri.service.BookService;
import top.shiyiri.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Aunean
 * @date 2022/2/8 20:53
 */
public class ClientBookServlet extends BaseServlet {
    private BookService service = new BookServiceImpl();
    /**
     * 处理分页功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("经过了前台的BookServlet程序");
        //1.获取请求的参数 pageNo 和 pageSize
        int pageNo = request.getParameter("pageNo") == null? 1 : (Integer.parseInt(request.getParameter("pageNo")));
        int pageSize = request.getParameter("pageSize") == null? Page.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
        //2.调用 service.page(pageNo, pageSize)：Page对象
        Page<Book> page = service.page(pageNo, pageSize);
        page.setUrl("client/bookServlet?action=page");
        //3.保存page对象到Request域中
        request.setAttribute("page", page);
        //4.请求转发到 /pages/manager/book_manager.jsp 页面
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }

    /**
     * 根据价格进行分页
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void pageByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("经过了前台的BookServlet程序");
        //1.获取请求的参数 pageNo 和 pageSize
        int pageNo = request.getParameter("pageNo") == null? 1 : (Integer.parseInt(request.getParameter("pageNo")));
        int pageSize = request.getParameter("pageSize") == null? Page.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
        int min = request.getParameter("min").equals("") ? 0 : Integer.parseInt(request.getParameter("min"));
        int max = request.getParameter("max").equals("") ? Integer.MAX_VALUE : Integer.parseInt(request.getParameter("max"));
        //2.调用 service.page(pageNo, pageSize)：Page对象
        System.out.println(min);
        System.out.println(max);
        Page<Book> page = service.pageByPrice(pageNo, pageSize, min, max);

        StringBuilder sb = new StringBuilder("client/bookServlet?action=pageByPrice");
        if (request.getParameter("min") != null) {
            sb.append("&min=").append(request.getParameter("min"));
        }
        if (request.getParameter("max") != null) {
            sb.append("&max=").append(request.getParameter("max"));
        }
        page.setUrl(sb.toString());
        //3.保存page对象到Request域中
        request.setAttribute("page", page);
        //4.请求转发到 /pages/manager/book_manager.jsp 页面
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }
}
